package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.systemctl.dao.SchoolSettingsDao;
import com.orhon.smartcampus.modules.systemctl.entity.Schoolsettings;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLSchoolSettingsInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.SchoolSettingsInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddSchoolSettingsMutation implements DataFetcher {

    @Autowired
    SchoolSettingsDao schoolSettingsDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 新增/修改学校设定信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLSchoolSettingsInput input = SchoolSettingsInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Schoolsettings schoolsettings = null;

        if (input.getId() != null) {
            schoolsettings = schoolSettingsDao.get(Schoolsettings.class, input.getId());
            schoolsettings.setUpdatedAt(new Date());
        } else {
            schoolsettings = new Schoolsettings();
            schoolsettings.setCreatedAt(new Date());
        }
        if(schoolId != null) {
            schoolsettings.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class, input.getSchool_id()));
        }
        if(input.getPlatform_name() != null){

            schoolsettings.setPlatformName( JSON.toJSONString(input.getPlatform_name()));
        }
        if(input.getGrades() != null){

            schoolsettings.setGrades(JSON.toJSONString(input.getGrades()));
        }
        if(input.getPeriods() != null){

            schoolsettings.setPeriods(JSON.toJSONString(input.getPeriods()));
        }
        if(input.getSubjects() != null){

            schoolsettings.setSubjects(JSON.toJSONString(input.getSubjects()));
        }
        if(input.getDuties() != null){

            schoolsettings.setDuties(JSON.toJSONString(input.getDuties()));
        }
        if(input.getCourses() != null){

            schoolsettings.setCourses(JSON.toJSONString(input.getCourses()));
        }
        if(input.getLogo() != null){

            schoolsettings.setLogo(input.getLogo());
        }
        if(input.getFavicon() != null){

            schoolsettings.setFavicon(input.getFavicon());
        }
        if(input.getTheme_settings() != null){

            schoolsettings.setThemeSettings(JSON.toJSONString(input.getTheme_settings()));
        }
        if(input.getMeta() != null){

            schoolsettings.setMeta( JSON.toJSONString(input.getMeta()));
        }
        try{
            schoolSettingsDao.saveOrUpdate(schoolsettings);
            Schools findById = schoolsDao.findById(input.getSchool_id());
            findById.setCloud_status(1);
            schoolsDao.saveOrUpdate(findById);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

